﻿using D.Medicalcare.Domain.Entities;
using D.Medicalcare.ErrorCode;
using D.Medicalcare.Infrastructure.Interfaces;
using D.Medicalcare.Read.API.Application.Command.RegistrationCommand;
using D.Medicalcare.Read.API.Dtos;
using MediatR;

namespace D.Medicalcare.Read.API.Application.Handler.RegistrationHandler
{
    /// <summary>
    /// 获取就诊人挂号信息
    /// </summary>
    public class GetRegistrationPatientsHandler : IRequestHandler<GetRegistrationPatientsCommand, APIResult<List<RegistrationDTO>>>
    {
        private readonly IBaseRepository<Registration> rrep;
        private readonly IBaseRepository<DoctorScheduling> srep;
        private readonly IBaseRepository<Doctor> drep;
        private readonly IBaseRepository<Department> prep;
        private readonly IBaseRepository<TitleManagement> trep;
        private readonly IBaseRepository<HospitalNavigation> hrep;
        private readonly IBaseRepository<Patient> patientrep;
        private readonly ILogger<GetRegistrationPatientsHandler> logger;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="rrep"></param>
        /// <param name="srep"></param>
        /// <param name="drep"></param>
        /// <param name="prep"></param>
        /// <param name="trep"></param>
        /// <param name="hrep"></param>
        /// <param name="patientrep"></param>
        public GetRegistrationPatientsHandler(IBaseRepository<Registration> rrep, IBaseRepository<DoctorScheduling> srep,
            IBaseRepository<Doctor> drep, IBaseRepository<Department> prep, IBaseRepository<TitleManagement> trep,
            IBaseRepository<HospitalNavigation> hrep, IBaseRepository<Patient> patientrep, ILogger<GetRegistrationPatientsHandler> logger)
        {
            this.rrep = rrep;
            this.srep = srep;
            this.drep = drep;
            this.prep = prep;
            this.trep = trep;
            this.hrep = hrep;
            this.patientrep = patientrep;
            this.logger = logger;
        }
        /// <summary>
        /// 查询挂号信息
        /// </summary>
        public Task<APIResult<List<RegistrationDTO>>> Handle(GetRegistrationPatientsCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<RegistrationDTO>> result = new APIResult<List<RegistrationDTO>>();
            try
            {
                var m = from a in rrep.GetAll()
                        join b in drep.GetAll() on a.DoctorId equals b.DoctorId into doctorJoin
                        from b in doctorJoin.DefaultIfEmpty()
                        join c in srep.GetAll() on b.DoctorId equals c.DoctorId into schedulingJoin
                        from c in schedulingJoin.DefaultIfEmpty()
                        join d in prep.GetAll() on b.DeparId equals d.DeparId into departmentJoin
                        from d in departmentJoin.DefaultIfEmpty()
                        join e in trep.GetAll() on b.TitlemanagementId equals e.TitleManagementId into titleJoin
                        from e in titleJoin.DefaultIfEmpty()
                        join f in hrep.GetAll() on d.CampusId equals f.HospitalNavigationId into hospitalJoin
                        from f in hospitalJoin.DefaultIfEmpty()
                        join g in patientrep.GetAll() on a.PatientId equals g.PatientId into patientJoin
                        from g in patientJoin.DefaultIfEmpty()
                        select new RegistrationDTO
                        {
                            PatientId = a.PatientId,
                            RegistrationId = a.RegistrationId,
                            CampusName = f.CampusName,
                            AddressCampus = f.AddressCampus,
                            PatientName = g.PatientName,
                            PatientNo = g.PatientNo,
                            DocumentNo = g.DocumentNo,
                            Phone = g.Phone,
                            DeparId = d.DeparId,
                            DeparName = d.DeparName,
                            DoctorName = b.DoctorName,
                            JobName = e.JobName,
                            Registrationfee = b.Registrationfee,
                            RegistrationType = a.RegistrationType,
                            Statue = a.Statue,
                            RegistrationTime = a.RegistrationTime,
                            Mtime = a.Mtime,
                            WaitTime = a.WaitTime,
                            UpdateTime = a.UpdateTime,
                            UpdateReson = a.UpdateReson,
                            Doctorlocation = d.Doctorlocation
                        };
                if (request.PatientId>0)
                {
                    m = m.Where(x => x.PatientId == request.PatientId);
                }
                if (request.RegistrationId>0)
                {
                    m = m.Where(x=>x.RegistrationId==request.RegistrationId);
                }
                logger.LogInformation("查询成功");
              result.Data =m.ToList();
                return Task.FromResult(result);
            }
            catch (Exception ex)
            {
                logger.LogError($"Error: {ex.Message}");
                throw;
            }

        }
    }
}
